package kr.hkit.androidcomplete02.ch19;

import kr.hkit.androidcomplete02.R;
import kr.hkit.androidcomplete02.ch19.HandlerEx.BackThread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class PostEx extends Activity {
	int mainValue = 0;
	int backValue = 0;
	TextView mMainText;
	TextView mBackText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_threadtest);

		mMainText = (TextView) findViewById(R.id.ch19_mainvalue);
		mBackText = (TextView) findViewById(R.id.ch19_backvalue);

		BackThread thread = new BackThread();
		thread.setDaemon(true);
		thread.start();

	}

	// 메인 스레드
	public void mOnClick(View v) {

		if (v.getId() == R.id.ch19_increase) {
			mainValue++;
			mMainText.setText("MainValue : " + mainValue);
			mBackText.setText("BackValue : " + backValue);
		}
	}

	Handler mHandler = new Handler();
	
	// 작업 스레드 (큐 공간에 증가하는 backValue 값을 넣어줌.)
	class BackThread extends Thread {

		public void run() {
			while (true) {
				backValue++;
				// post를 이용해서 코드를 전송
				mHandler.post(new Runnable() {
					
					@Override
					public void run() {
						mBackText.setText("BackValue : " + backValue);
					}
				});
				
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}
		}
	}
}
